package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Command;
import model.IAction;
import model.PlayerInfo;
import service.GlobalService;
import utilities.GameResource;
import utilities.TField;

public class MsgScreen extends Screen {
	public Vector tabs = new Vector();
	public Screen lastScr;
	//public ChatTab publicTab;
	public int selectedTab;
	public TField tfChat;
	static MsgScreen instance;
	public static MsgScreen gI(){
		if(instance==null)
			instance=new MsgScreen();
		return instance;       
	}
	boolean showTfChat = true;
        PlayerInfo m_playerInfo;
    boolean flag;

	public void switchToMe(Screen lastScr) {		
		this.lastScr = lastScr;
		//Screen.nMsg = 0;				
		if(tabs.size() > 0){
			MsgScreen.gI().selectedTab = MsgScreen.gI().tabs.size()-1;
			PaintPopup.gI().setInfo(getTab(selectedTab).title,GameCanvas.w - X * 2, GameCanvas.h - Y * 2, tabs.size());				
			PaintPopup.gI().focusTab=selectedTab;		
			MsgScreen.gI().setChangeTabEnd();
			showTfChat = getTab(selectedTab).showChatTf;		
		}
		super.switchToMe();
	}	
	public MsgScreen() {
		tfChat = new TField();
		tfChat.x = X + 5;
		tfChat.y = GameCanvas.h - Screen.ITEM_HEIGHT - Y - 7;
		tfChat.width = GameCanvas.w - X * 2 - 30;
		tfChat.height = Screen.ITEM_HEIGHT + 2;
		tfChat.isFocus = true;
		tfChat.setMaxTextLenght(40);
		selectedTab = 0;
		m_cmdLeft=new Command(GameResource.menu,new IAction(){
			public void perform(){	
				Vector menu=new Vector();
				if(selectedTab!=0)
					menu.addElement(new Command(GameResource.closeTab,new IAction(){
						public void perform(){
							removeTab(getTab(selectedTab));
						}
					}));
				
				menu.addElement(new Command(GameResource.close, new IAction() {
					public void perform() {							
						close();
                                                }
				}));
				GameCanvas.menu.startAt(menu , 0);
			}
		});		
		
		/////////add admin tab
		ChatTab	tab = new ChatTab(GameResource.admin, -9999, null, null, false);
		addTab(tab);
			
		setChangeTabEnd();
	}

	public void close(){
		lastScr.switchToMe();
        //System.gc();
	}
	
	public void setChangeTabEnd() {
		if(tabs.size() == 0)
			return;
		getTab(selectedTab).isColor=false;
		this.m_cmdCenter = getTab(selectedTab).center;
		this.m_cmdRight = getTab(selectedTab).right;
		tfChat.setText(getTab(selectedTab).currentChatText);
		showTfChat = getTab(selectedTab).showChatTf;	
		getTab(selectedTab).reset();
		if(GameCanvas.currentScreen==this)
		PaintPopup.gI().setNameAndFocus(getTab(selectedTab).title, selectedTab);
	}

	private void setChangeTabStart() {
		getTab(selectedTab).currentChatText = tfChat.getText();		
	}
	
	public void onChatFrom(int id,String name,String info) {		
		ChatTab tab = findTab(id);		
		if (tab == null) {
			tab = new ChatTab(name, id, new Command(GameResource.chat, new IAction() {
				public void perform() {
					doSendMsg();
				}
			}), tfChat.cmdClear, true);
			addTab(tab);					
		}else{
			tab.isColor=true;
		}
		tab.addText(name,info);				
	}
	public void addRequest(ChatTab tab){
		tab.isColor=true;
		addTab(tab);
	}
	protected void doSendMsg() {
		if (tfChat.getText().equals(""))
			return;		
		ChatTab ct = getTab(selectedTab);
		String text = tfChat.getText();		
		
		//send to server
		GlobalService.gI().doChatWidth(ct.opponentID, text);
		tfChat.setText("");
	
		ct.addText(NuoiThuMidlet.m_myPlayerInfo.m_strName + ": " + text);
	}

	public ChatTab findTab(int id) {
                int a = tabs.size();
		for (int i = 0; i < a; i++) {
			if (((ChatTab) tabs.elementAt(i)).opponentID == id)
				return (ChatTab) tabs.elementAt(i);
		}
		return null;
	}

	public ChatTab getTab(int index) {
		if(index < tabs.size())
			return (ChatTab) tabs.elementAt(index);
		else
			return null;
	}

	public static int X = 20, Y = 20;

	public void paint(Graphics g) {		
		lastScr.paint(g);

        g.translate(-g.getTranslateX(), -g.getTranslateY());
        PaintPopup.gI().paintTabs(g);
        g.translate(X, Y + 22);
        
        ChatTab chatTab = getTab(selectedTab);
        if(chatTab != null){
        	chatTab.paint(g);
	        if (showTfChat) {
	            g.translate(-g.getTranslateX(), -g.getTranslateY());

	    		tfChat.y = GameCanvas.h - Screen.ITEM_HEIGHT - Y - 7;
	            tfChat.paint(g);
	        }
	    }
        
        super.paint(g); 
	}
	
	public void keyPress(int keyCode) {
		if (keyCode == -3) {
			setChangeTabStart();
			selectedTab--;
			if (selectedTab < 0)
				selectedTab = tabs.size() - 1;
			setChangeTabEnd();
			getTab(selectedTab).initScroll();
		}
		if (keyCode == -4) {
			setChangeTabStart();
			selectedTab++;
			if (selectedTab >= tabs.size())
				selectedTab = 0;
			setChangeTabEnd();
			getTab(selectedTab).initScroll();
		}
		if (showTfChat)
			tfChat.keyPressed(keyCode);
		super.keyPress(keyCode);
	}

	public void updateKey() {
		if(GameCanvas.keyHold[2])getTab(selectedTab).update(-1);		
		if(GameCanvas.keyHold[8])getTab(selectedTab).update(1);
		super.updateKey();
	}

	public void update() {		
		if (showTfChat)
			tfChat.update();
		lastScr.update();		
	}

	public void removeTab(ChatTab ct) {
		tabs.removeElement(ct);		
		if (selectedTab >= tabs.size())
			selectedTab = tabs.size() - 1;
		PaintPopup.gI().focusTab=selectedTab;
		PaintPopup.gI().setNumTab(tabs.size());
		setChangeTabEnd();
	}

	public void startChatTo(int iDDB, String name) {
            flag = false;
		ChatTab tab = findTab(iDDB);		
		if (tab == null) {
			tab = new ChatTab(name, iDDB, new Command(GameResource.chat, new IAction() {
				public void perform() {
					doSendMsg();
				}
			}), tfChat.cmdClear, true);
			addTab(tab);		
		}
		// Find this tab
                int a = tabs.size();
		for (int i = 0; i < a; i++) {
			if (tabs.elementAt(i) == tab)
				selectedTab = i;
		}
		setChangeTabEnd();
	}
    public void profileScr(PlayerInfo p){
        Command close =new Command(GameResource.close, new IAction() {
			public void perform() {							
				close();
                                       
			}
		});
        
        this.m_cmdLeft = close;
        flag =true;
        m_playerInfo = p;
        
        m_width = GameCanvas.w3d4;
		m_height = 160;
		m_left = (GameCanvas.w - m_width)/2;
		m_top = (GameCanvas.h - m_height)/2;
		
		int wC = GameCanvas.w - 30;
		if (wC < 70)
			wC = 70;
		if (wC > 99)
			wC = 99;
    }
       
	public void addTab(ChatTab tab){		
		tabs.addElement(tab);
		if(GameCanvas.currentScreen==this){			
			PaintPopup.gI().setNumTab(tabs.size());
		}
	}
	
	public void paintMarqueeMsg(Graphics g){
	}
}